package com.example.leddemo.activity;

import com.example.leddemo.R;
import com.example.leddemo.widget.ColorPickerDialog;

import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

	private EditText textsize;
	private EditText text;
	private String textString;
	private Button open;
	private TextView add;
	private TextView delete;
	private TextView colormore;
	private ColorPickerDialog dialog;
	private int color = Color.GREEN;
	private int textSize = 24;
	private TextView demo;
	private float speed = 10;
	private SeekBar seekBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		init();
		initListener();
	}

	private void initListener() {
		open.setOnClickListener(this);
		add.setOnClickListener(this);
		delete.setOnClickListener(this);
		colormore.setOnClickListener(this);

		textsize.addTextChangedListener(new MyWather());
		text.addTextChangedListener(new MyWather());
		
		seekBar.setOnSeekBarChangeListener(new MySeekBarChangeListener());
	}

	private void init() {

		textsize = (EditText)findViewById(R.id.et_size);
		text = (EditText)findViewById(R.id.et_text);
		open = (Button) findViewById(R.id.open);
		add = (TextView) findViewById(R.id.tv_add);
		delete = (TextView) findViewById(R.id.tv_delete);
		demo = (TextView) findViewById(R.id.tv_demo);

		colormore = (TextView)findViewById(R.id.colormore);
		seekBar = (SeekBar) findViewById(R.id.sb_speed);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.open:

			textString = demo.getText().toString().trim();
			Intent intent = new Intent(this,ScroActivity.class);
			intent.putExtra("color", color);
			intent.putExtra("text", textString);
			intent.putExtra("size", textSize);
			intent.putExtra("speed", speed );
			Log.i("syso", textString);
			startActivity(intent);
			break;

		case R.id.tv_add:
			String m = textsize.getText().toString().trim();
			textSize = Integer.parseInt(m)+1;
			textsize.setText(textSize+"");
			break;
		case R.id.tv_delete:
			String n = textsize.getText().toString().trim();
			textSize = Integer.parseInt(n)-1;
			textsize.setText(textSize+"");
			break;
		case R.id.colormore:
			dialog = new ColorPickerDialog(this, colormore.getTextColors().getDefaultColor(),   
					"",   
					new ColorPickerDialog.OnColorChangedListener() {  

				@Override  
				public void colorChanged(int color) {  
					colormore.setBackgroundColor(color);
					MainActivity.this.color = color;
					demo.setTextColor(color);
				}  
			}); 
			dialog.show(); 
			break;
		}

	}

	class MyWather implements TextWatcher{

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			String textString = text.getText().toString().trim();
			if (TextUtils.isEmpty(textString)) {
				demo.setText("demo");;
			}else{
				demo.setText(textString);
			}
			String size = textsize.getText().toString().trim();
			if (TextUtils.isEmpty(size)) {
				return;
			}
			textSize = Integer.parseInt(size);
			demo.setTextSize(textSize);
		}
	}
	
	class MySeekBarChangeListener implements OnSeekBarChangeListener{

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			speed = progress/10;
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			
		}
	}

}
